/**
* This file is used to setup configuration file,
* send and receive data between networks,
* log messages to file, and encrypt passwords
*/
#ifndef UTILS_H
#define UTILS_H

//-------------------------------------------------------
// Header Files
//-------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h> // For crypt()
#include "storage.h"

//-------------------------------------------------------
// Constants
//-------------------------------------------------------
// comment character for configuration file
// Note: Can only be placed on a new line by itself and not after anything
static const char CONFIG_COMMENT_CHAR = '#';
#define MAX_CMD_LEN (1024 * 8) // maximum command's length given by client to server
#define LOG(x)  {printf x; fflush(stdout);} // printf x to stdout, flush without needing '\n' character
					    // Note: x will contain the braces as well ("...",...)
#ifdef NDEBUG
#define DBG(x)  {}
#else
#define DBG(x)  {printf x; fflush(stdout);}
#endif

// A structure to store the configuration file's parameters
// Note: Does not store the table data currently
struct config_params
{
    char server_host[MAX_HOST_LEN];
    int server_port;
    char username[MAX_USERNAME_LEN];
    char password[MAX_ENC_PASSWORD_LEN];
    //char data_directory[MAX_PATH_LEN];
};

//-------------------------------------------------------
// Utility Functions
//-------------------------------------------------------
static inline void die(char *msg, int code)
{
    printf("%s\n", msg);
    exit(code);
}
int sendall(const int sock, const char *buf, const size_t len);
int recvline(const int sock, char *buf, const size_t buflen);
int read_config(const char *config_file, struct config_params *params);
void logger(FILE *file, char *message);
#define DEFAULT_CRYPT_SALT "xx"
char *generate_encrypted_password(const char *passwd, const char *salt);
#endif
